package com.iris.live.services.services.impl;

import com.iris.live.services.data.models.团内客户表Model;
import com.iris.live.services.data.repositories.GroupCustomerRepository;
import com.iris.live.services.data.repositories.团内客户表Repository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.models.ResultData;
import com.iris.live.services.services.GroupCustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: luojie
 * date: 2017/12/15 15:08
 */
@Service
public class GroupCustomerServiceImpl implements GroupCustomerService {

    private static Logger logger = LoggerFactory.getLogger(GroupCustomerServiceImpl.class);

    @Autowired
    private 团内客户表Repository groupCustomerRepository;

    @Autowired
    private GroupCustomerRepository gcRepository;

    /**
     * 查询团内客户列表
     *
     * @param permit
     * @return
     */
    @Override
    public ResultData getGroupCustomerList(String permit, Map map) {
        PageData pageData = null;
        try {
            map.put("permit", permit);
            pageData = gcRepository.findAllByPermit(map);
        } catch (Exception e) {
            logger.error("团内客户列表查询失败", e);
            return new ResultData("false", "查询失败");
        }
        return new ResultData("true", "查询成功", pageData);
    }

    /**
     * 新增团内客户
     *
     * @param permit
     * @param user
     * @param groupCustomer
     * @return
     */
    @Transactional
    @Override
    public ResultData addGroupCustomer(String permit, String user, 团内客户表Model groupCustomer) {
        try {
            团内客户表Model model = groupCustomerRepository.findByCompanyNameAndCreditCode(permit, groupCustomer.get企业名称(), groupCustomer.get社会信用代码());
            if (model == null) {
                groupCustomer.set许可(permit);
                groupCustomer.set更新账号(user);
                groupCustomer.set状态("已启用");
                groupCustomer.set创建时间(new Timestamp(System.currentTimeMillis()));
                groupCustomer.set更新时间(new Timestamp(System.currentTimeMillis()));
                groupCustomerRepository.save(groupCustomer);
            } else {
                return new ResultData("false", "团内客户已存在");
            }
        } catch (Exception e) {
            logger.error("团内客户保存失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResultData("false", "保存失败");
        }
        return new ResultData("true", "保存成功");
    }

    /**
     * 修改团内客户的状态
     *
     * @param map
     * @param status
     * @return
     */
    @Transactional
    @Override
    public ResultData updateGroupCustomerStatus(List<Map> map, int status) {
        try {
            List list = new ArrayList();
            for (int i = 0; i < map.size(); i++) {
                Map m = map.get(i);
                list.add(Integer.valueOf(m.get("编号").toString()));
            }
            groupCustomerRepository.updateGroupCustomerStatus(status == 0 ? "已启用" : "已停用", list);
        } catch (Exception e) {
            logger.error("修改团内客户状态失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResultData("false", "保存失败");
        }
        return new ResultData("true", "保存成功");
    }
}